/**
 * Created by DELL on 2017/5/23.
 */
var jq = $.noConflict();
//密码修改页面控制器
var appReg = angular.module('appTeacher', ['ngResource', 'ngRoute', 'oc.lazyLoad', 'ngCookies']);
appReg.controller("regCtr", function ($scope, $resource, $cookieStore) {
    $scope.username = $cookieStore.get("loginInfo").name;
    $scope.loginOut = function () {
        $cookieStore.remove('loginInfo');
        window.location.href = '../../login.html';
    };
    $scope.postdata = {
        oldPsd: '',
        newPsd: '',
        newPsdRe: ''
    };
    var service = {
        getmsg: function (content) {
            if (content.isSuccess && content.dataCount === 0) {
                jq("#regErr1").css({"color": "red"});
                $scope.errmsg = "旧密码错误";
            }
            else if (content.isSuccess && content.dataCount > 0) {
                jq("#regErr1").css({"color": "green"});
                $scope.errmsg = content.message;
            }
            else {
                jq("#regErr1").css({"color": "red"});
                $scope.errmsg = "修改异常，刷新页面试试吧！";
            }
        }
    };
    $scope.errmsg = '';
    $scope.modify = function () {
        if ($scope.postdata.oldPsd === '' || $scope.postdata.newPsd === '' || $scope.postdata.newPsdRe === '') {
            jq("#regErr1").css({"color": "red"});
            $scope.errmsg = "以下三项必填！";
        }
        else if ($scope.postdata.oldPsd !== '' && $scope.postdata.oldPsd === $scope.postdata.newPsd) {
            jq("#regErr1").css({"color": "red"});
            $scope.errmsg = "新旧密码不能相同！";
        }
        else if ($scope.postdata.newPsd !=='' && $scope.postdata.newPsd !== $scope.postdata.newPsdRe) {
            jq("#regErr1").css({"color": "red"});
            $scope.errmsg = "新密码和确认密码不一致!";
        }
        else {
            var sentdata = {
                loginname: $cookieStore.get("loginInfo").username,
                oldpasswords: $scope.postdata.oldPsd,
                newpasswords: $scope.postdata.newPsd
            };
            httpServer.modifyPsd($resource, sentdata, service.getmsg);
        }
    };
});